Page({
  data: {
    merchantInfo: null,
    loading: false,
  },

  onLoad() {
    this.loadMerchantInfo();
  },

  // 加载商家信息
  async loadMerchantInfo() {
    this.setData({ loading: true });
    try {
      const { result } = await wx.cloud.callFunction({
        name: "getMerchantInfo",
      });

      if (result.code === 0) {
        this.setData({
          merchantInfo: result.data,
          loading: false,
        });
      } else {
        wx.showToast({
          title: result.msg || "加载失败",
          icon: "none",
        });
        this.setData({ loading: false });
      }
    } catch (err) {
      console.error("加载商家信息失败：", err);
      wx.showToast({
        title: "加载失败",
        icon: "none",
      });
      this.setData({ loading: false });
    }
  },

  // 跳转到菜品列表
  navigateToDishList() {
    if (!this.data.merchantInfo) return;
    wx.navigateTo({
      url: `/pages/merchant/dish/list?merchantId=${this.data.merchantInfo._id}`,
    });
  },
});
